import Vue from 'vue'
import Vuex from 'vuex'
import {getUserInfo, logout} from "@/api/api";

Vue.use(Vuex)

export default new Vuex.Store({
  //
  state: {
    showLogin:false,
    isLogin:false,
    userInfo:null
  },
  mutations: {
    //展示模态框
    changeShowLogin(state,payload){
      state.showLogin= payload
    },
  //更改个人信息
    changeUserInfo(state,payload){
      state.isLogin = payload.isLogin
      state.userInfo = payload.userInfo
    }
  },
  actions: {
    //查看是否登录
    //context上下文环境
    getUserIsLogin(context){
      getUserInfo().then(res=>{
        if (res.code == 0){
          context.commit("changeUserInfo",{isLogin:true,userInfo:res.userInfo})
        }else {
          // (res)
          context.commit("changeUserInfo",{isLogin:false,userInfo:{}})
        }
      })
    },
    lagOut(context){
      logout().then(res => {
        if (res.code == 0) {
          context.commit("changeUserInfo",{isLogin:false,userInfo:{}})
        }
      })
    }
  },
  modules: {
  }
})
